package cn.hy.考试;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main01 {


    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = Integer.parseInt(sc.nextLine());
        int[][] grid = new int[n][n];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                grid[i][j] = sc.nextInt();
            }
            sc.nextLine();
        }
        int k = Integer.parseInt(sc.nextLine()); // 服务
        int ans = dfs(grid, k - 1);
        System.out.println(ans);
    }

    public static int dfs(int[][] grid, int k) {
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < grid.length; i++) {
            if (k != i && grid[k][i] == 1) {
                list.add(dfs(grid, i));
            }
        }
        int max = 0;
        for (Integer times : list) { // 所依赖的服务最长时间
            max = Math.max(times, max);
        }
        return max + grid[k][k];
    }
}

